﻿using System;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Poet.Converters
{
    public class ImageSourceToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var imageSource = value as ImageSource;
            if (imageSource == null) return null;
            var uri = new Uri(string.Format(imageSource.ToString()));
            var image = new BitmapImage(uri);
            var width = imageSource.Width;
            var height = imageSource.Height;

            double size;
            if (double.TryParse(parameter as string, out size))
                width = height = size;

            return new Image {Source = image, Width = width, Height = height};
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
